package com.example.ribbon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.DataNode;
import com.example.auth.domain.User;
import com.example.config.auth.SecurityUtil;
import com.example.config.exception.GlobalExcetion;
import com.example.config.ribbon.ServiceCaller;
import com.example.feign.NodeFeignService;
import com.example.feign.UserFeignService;
import com.example.ribbon.Constant;
import com.example.ribbon.service.ReceiveMsgRibbonService;
import com.example.util.constant.NodeUrlConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class ReceiveMsgRibbonServiceImpl implements ReceiveMsgRibbonService {

    @Autowired
    @Lazy
    ServiceCaller serviceCaller;

    @Autowired
    private NodeFeignService nodeFeignService;

    @Autowired
    private UserFeignService userFeignService;

    public JSONObject receiveMsg(String toUid,String msg) throws GlobalExcetion {
        if(toUid==null || msg==null){
            throw new GlobalExcetion(1,"必要参数不得为空");
        }
        String fromUserId= SecurityUtil.getUserId();
        User toUser=userFeignService.getUserByUserId(toUid);
        User fromUser=userFeignService.getUserByUserId(fromUserId);
        DataNode dataNode =userFeignService.getUserNodeByNodeNoUserId(toUser.getUuid(), Constant.DATA_SERVER_NAME);
        JSONObject req=new JSONObject();
        req.put("fromUid",fromUser.getUuid());
        req.put("fromNodeid",fromUser.getNodeId());
        req.put("toUid",toUser.getUuid());
        req.put("toNodeid",toUser.getNodeId());
        req.put("msgContent",msg);
        req.put("msgType","1");
        return serviceCaller.post(dataNode.getNodeServerName(),NodeUrlConstant.RECEIVE_RECEIVEMSG,req,JSONObject.class);
    }

    @Override
    public JSONObject getUserMsgList() {
        return serviceCaller.getByUserServiceName(NodeUrlConstant.RECEIVE_GETMSGLIST,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject getUserMsgPage(Integer pageNum, Integer pageSize) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("pageNum",pageNum.toString());
        params.add("pageSize",pageSize.toString());
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.RECEIVE_GETMSGLIST_PAGE)
                .queryParams(params)
                .toUriString();
        return serviceCaller.getByUserServiceName(url,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject getUserMsgPage(Integer pageNum, Integer pageSize, String formUid) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("pageNum",pageNum.toString());
        params.add("pageSize",pageSize.toString());
        params.add("formUid", formUid);
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.RECEIVE_GETMSGLIST_PAGE_FORMUID)
                .queryParams(params)
                .toUriString();
        return serviceCaller.getByUserServiceName(url,JSONObject.class,new JSONObject());
    }
}
